//
//  LoginViewController.m
//  credit
//
//  Created by scj on 2018/1/14.
//  Copyright © 2018年 scj. All rights reserved.
//

#import "LoginViewController.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import <AVOSCloud/AVOSCloud.h>
//#import "LoginDao.h"
//#import "XPFTabBarController.h"
//#import "RegistStep1ViewController.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"登陆";
    
    //    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    //    if ([defaults objectForKey:@"account"]) {
    //        loginfield.text=[defaults objectForKey:@"account"];
    //    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)login:(id)sender{
    if (loginfield.text.length!=11) {
        [SVProgressHUD showErrorWithStatus:@"请输入11位手机号"];
        return;
    }
    if (pasfield.text.length!=6) {
        [SVProgressHUD showErrorWithStatus:@"请输入6位密码"];
        return;
    }
    __weak typeof(self)weakSelf=self;
    [AVUser logInWithUsernameInBackground:loginfield.text password:pasfield.text block:^(AVUser *user, NSError *error) {
        
        if (user != nil) {
            //                    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            //                    [defaults setObject:loginfield.text forKey:@"account"];
            //                    [defaults setObject:pasfield.text forKey:@"password"];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } else {
            if ([[error userInfo] objectForKey:@"error"]) {
                [SVProgressHUD showErrorWithStatus:([[error userInfo] objectForKey:@"error"])];
            }
            
        }
    }];
    
}

-(void)viewWillAppear:(BOOL)animated{
    AVUser *currentUser = [AVUser currentUser];
    if (currentUser != nil) {
        loginfield.text=currentUser.username;
    }
}

-(IBAction)regist:(id)sender{
    UIStoryboard *jump=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    LoginViewController *loginViewController=[jump instantiateViewControllerWithIdentifier:@"RegistViewController"];
    loginViewController.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:loginViewController animated:YES];
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
